﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppEnvironment.Lib.Interfaces;

namespace AppEnvironment.Lib
{
    public class AppCurrentThread
    {
        internal AppCurrentThread()
        {
            ForTypeList = new SortedDictionary<string, AppForType>();
        }

        public object this[string name]
        {
            get { return App.ContextItems[name]; }
            set { App.ContextItems[name] = value; }
        }

        public IStateHolder Items { get { return App.ContextItems; } }

        SortedDictionary<string, AppForType> ForTypeList;
        public AppForType<T> ForType<T>()
        {
            AppForType item = null;
            if (!ForTypeList.TryGetValue(typeof(T).FullName, out item))
            {
                item = new AppForType<T>();
                ForTypeList[typeof(T).FullName] = item;
            }
            return item as AppForType<T>;
        }

        public void SetOverride<TFromType, TToType>()
        {
            App.SetOverrideForThread<TFromType, TToType>();
        }

        public void SetOverride<TFromType>(string toType)
        {
            App.SetOverrideForThread<TFromType>(toType);
        }

        public void SetOverride<TFromType>(Type toType)
        {
            App.SetOverrideForThread<TFromType>(toType);
        }

        public void ClearOverrides()
        {
            App.ClearOverridesForThread();
        }
    }
}
